package walkera.cn.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.wkgame.tetris.R;

import io.reactivex.Observable;
import walkera.cn.tool.WkHeartBeatTool;
import walkera.cn.view.GameView;
import walkera.cn.view.LongPressBtn;

public class PlayActivity extends Activity {

    private GameView gameView;
    private TextView tv_score;
    private WkHeartBeatTool wkHeartBeatTool = new WkHeartBeatTool() ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_play);
        initView();
    }



    private void initView() {
        gameView =  findViewById(R.id.gameView);
        tv_score =  findViewById(R.id.tv_score);

        LongPressBtn btn_rotate = findViewById(R.id.btn_rotate);
        LongPressBtn btn_move_left = findViewById(R.id.btn_move_left);
        LongPressBtn btn_move_right = findViewById(R.id.btn_move_right);
        LongPressBtn btn_move_quick = findViewById(R.id.btn_move_quick);

        btn_rotate.init(wkHeartBeatTool,  onLongTouchResult ,R.mipmap.p1 ,R.mipmap.p1_press);
        btn_move_left.init(wkHeartBeatTool,  onLongTouchResult ,R.mipmap.p1 ,R.mipmap.p1_press);
        btn_move_right.init(wkHeartBeatTool,  onLongTouchResult ,R.mipmap.p1 ,R.mipmap.p1_press);
        btn_move_quick.init(wkHeartBeatTool,  onLongTouchResult ,R.mipmap.p1 ,R.mipmap.p1_press);


        gameView.setGameListener(new GameView.GameListener() {
            @Override
            public void onGameState(int score, boolean isOver) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(isOver){
                            tv_score.setText( String.format("游戏结束%s" ,tv_score.getText().toString())  );
                        }else{
                            tv_score.setText( String.format("得分%s" ,tv_score.getText().toString())  );
                        }

                    }
                });
            }
        });
    }




    private LongPressBtn.OnLongTouchResult onLongTouchResult = new LongPressBtn.OnLongTouchResult() {
        @Override
        public void getLongAction(int viewId) {
            switch (viewId){
                case R.id.btn_rotate:
                    gameView.rotate();
                    break;
                case R.id.btn_move_left:
                    gameView.moveLeft();
                    break;
                case R.id.btn_move_right:
                    gameView.moveRight();
                    break;
                case R.id.btn_move_quick:
                    gameView.setQuikDown(true) ;
                    break;
            }

        }

        @Override
        public void onStopPress(int viewId) {
            if (viewId == R.id.btn_move_quick) {
                gameView.setQuikDown(false);
            }

        }
    };
}
